import { get, post } from '@/utils/request'

/**
 * 文章相关接口
 */
export const articleApi = {
  // 获取文章列表
  getList: (params) => post('/articleApi/getList', params),

  // 获取文章详情
  getDetail: (id) => post('/articleApi/details', null, { params: { id } }),

  // 新增文章
  add: (data) => post('/articleApi/add', data),

  // 发布文章
  publish: (data) => {
    // 构造请求数据
    const articleData = {
      title: data.title,
      picUrl: data.picUrl,
      description: data.summary,
      type: data.type,
      password: data.password,
      status: data.status,
      categoryId: data.category || '1',
      orderNum: data.orderNum || 0,
      content: data.content,
      tags: data.tags || [], // 直接使用标签名称数组
    }

    return post('/articleApi/publish', articleData)
  },

  // 保存草稿
  saveDraft: (data) => {
    // 构造草稿数据
    const draftData = {
      title: data.title,
      content: data.content,
      picUrl: data.picUrl,
      description: data.summary,
      type: data.type,
      password: data.password,
      status: '0',
      categoryId: data.category || '1',
      orderNum: data.orderNum || 0,
      tags: data.tags || [], // 直接使用标签名称数组
    }

    return post('/articleApi/publish', draftData)
  },

  // 更新文章
  edit: (data) => post('/articleApi/edit', data),

  // 删除文章（可批量）
  delete: (ids) => post('/articleApi/delete', { ids }),

  // 修改排序
  updateOrder: (id, orderNum) => post('/articleApi/editOrder', { id, orderNum }),

  // 导出文章为Markdown
  exportMarkdown: (id) =>
    post('/articleApi/exportMarkdown', null, {
      params: { id },
      responseType: 'blob', // 设置响应类型为blob以接收文件
    }),

  // 更新文章推荐状态
  updateRecommend: (id, isRecommend) =>
    post('/articleApi/updateRecommend', null, {
      params: { id, isRecommend },
    }),

  // AI生成摘要
  autoSummary: (data) => post('/articleApi/autoSummary', data),
}
